var uriUtil = require("../../utils/uri.js");
var httpUtil = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: uriUtil.BASE_URL,
    baseImgUrl: uriUtil.BASE_URL_UPLOAD,
    goodsTypeList:  [],
    goodsList:  [],
    currentGoodsTabPos: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.onPullDownRefresh();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.getBookTypleList();
  },

  /**
   * 获取图书分类列表
   */
  getBookTypleList: function(){
    var that = this;
    httpUtil.postReq(uriUtil.GOODSTYPELIST,{
      state: 1
    },function(res){
      if(res){
        that.setData({
          goodsTypeList: res.data
        });
        that.getBookList(res.data[0]['id']);
      }
    },false,true);
  },

  /**
   * 点击图书分类
   */
  onGoodsTypeClick: function(e){
    let that = this;
    let pos = e.currentTarget.dataset.index;
    that.setData({
      currentGoodsTabPos : pos
    });
    let item = that.data.goodsTypeList[pos];
    that.getBookList(item['id']);
  },

  /**
   * 获取某个图书分类的图书列表
   */
  getBookList: function(typeId){
    var that = this;
    httpUtil.postReq(uriUtil.GOODSLIST,{
      state: 1,
      typeId: typeId
    },function(res){
      that.setData({
        goodsList: res.data
      });
    },false,true);
  },

  /**
   * 转向图书详情页面
   */
  onBookClick: function(e){
    let that = this;
    let pos = e.currentTarget.dataset.index;
    let item = that.data.goodsList[pos];
    wx.navigateTo({
      url: '../../pages/bookDetail/bookDetail?id=' + item.id,
    })
  }

})